<?php
namespace app\lawcase\model;

use think\Model;

class CourtPhone extends Model
{
    // 设置当前模型对应的数据表名称
    protected $name = 'court_phone';
    // 设置数据表前缀
    protected $prefix = 'oa_';
    protected $autoWriteTimestamp = false; // 不自动写入时间戳，手动处理时间戳

    /**
     * 获取法院电话列表
     * @param int $court_id
     * @return array
     */
    public function getPhones(int $court_id)
    {
        try {
            $list = $this->where('court_id', $court_id)
                ->where('delete_time', 0)
                ->order('is_default desc, create_time asc')
                ->select();

            return $list;
        } catch (\Exception $e) {
            return to_assign(31, '获取电话列表失败：'. $e->getMessage());
        }
    }


    /**
     * 添加法院电话
     * @param array $param
     * @return array
     */
    public function addPhone(array $param)
    {
        try {
            $param['create_time'] = time();
            $param['update_time'] = time();
            $insertId = self::strict(false)
                ->field(['court_id','phone','description','is_default','create_time','update_time'])
                ->insertGetId($param);
            add_log('add', $insertId, $param);
        }catch (\Exception $e) {
            return to_assign(1, '添加失败：'. $e->getMessage());
        }
        return to_assign(0, '添加成功', ['id' => $insertId]);
    }

    /**
     * 编辑法院电话
     * @param array $param
     * @return array
     */
    public function editPhone($id, array $param)
    {
        try {
            $id = $param['id'];
            unset($param['id']);
            // 手动设置时间戳
            $param['update_time'] = time();
            $this->where('id', $id)->update($param);

            add_log('edit', $id, $param);
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => '编辑失败：' . $e->getMessage()];
        }
        return to_assign(0, '编辑成功', ['id' => $id]);
    }

    /**
     * 设置默认电话
     * @param int $id
     * @param int $court_id
     * @return array
     */
    public function setDefault(int $id, int $court_id)
    {
        try {
            // 先将所有电话设置为非默认
            $this->where('court_id', $court_id)->update(['is_default' => 0]);
            // 设置指定电话为默认
            $result = $this->where('id', $id)->update(['is_default' => 1]);
            if ($result) {
                return ['code' => 0, 'msg' => '设置默认成功'];
            } else {
                return ['code' => 1, 'msg' => '设置默认失败'];
            }
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => '设置默认失败：' . $e->getMessage()];
        }
    }

    /**
     * 删除法院电话
     * @param int $id
     * @return array
     */
    public function removePhone(int $id)
    {
        try {
            // 设置 delete_time 为当前时间戳
            $result = $this->where('id', $id)->update(['delete_time' => time()]);
            if ($result) {
                return ['code' => 0, 'msg' => '删除成功'];
            } else {
                return ['code' => 1, 'msg' => '删除失败'];
            }
        } catch (\Exception $e) {
            return ['code' => 1, 'msg' => '删除失败：' . $e->getMessage()];
        }
    }

    /**
     * 获取单个电话信息
     * @param int $id
     * @return array|null
     */
    public function getPhone(int $id)
    {
        $phone = $this->where('id', $id)->find();
        if ($phone) {
            return ['code' => 0, 'msg' => '获取成功', 'data' => $phone];
        } else {
            return ['code' => 1, 'msg' => '电话信息不存在'];
        }
    }

}